package dai.engine

import dai.core.base.*
import dai.core.base.camera.Camera
import dai.core.base.loader.ObjLoader
import org.joml.Vector3f
import org.lwjgl.glfw.GLFW.*
import org.lwjgl.opengl.GL11C.*


/**
 *  绘制一个矩形
 */
class DrawTriangle : IGameLogic {

    val renderer: Renderer = Renderer()
//    lateinit var mesh: Mesh

    val items = ArrayList<GameItem>()
    var cameraInc = Vector3f()
    val camera = Camera(Vector3f(0f,1f,3f))
    override fun init(window: Window) {
        renderer.init(window)

//        val houseMesh: List<Mesh> = StaticMeshLoader.load("models/house/house.obj", "models/house")
//        MyLog.info("houseMesh:${houseMesh.size}")
//        houseMesh.forEach { mesh->
//            val item = GameItem(mesh)
//            items.add(item)
//        }

        val cube = ObjLoader.loadMesh("/models/bunny.obj")
        cube.material = Material(texture = TextureCache.getTexture("textures/ball.png"))
        val cubeItem = GameItem(cube)
        cubeItem.position.z = -10f
        items.add(cubeItem)

//        val rabbit = ObjLoader.loadMesh("/models/bunny.obj")
//        val rabbitItem = GameItem(rabbit)
//        rabbitItem.position.x = 3f
//        rabbitItem.position.z = -5f
//        items.add(rabbitItem)

    }

    override fun input(window: Window) {
        cameraInc.set(0f, 0f, 0f)
        when {
            // escape
            window.isKeyPress(GLFW_KEY_ESCAPE) -> glfwSetWindowShouldClose(window.winHandler, true)

            // 前进
            window.isKeyPress(GLFW_KEY_UP) ||
                    window.isKeyPress(GLFW_KEY_W) -> {
                cameraInc.z = -1f
            }

            // 后退
            window.isKeyPress(GLFW_KEY_DOWN) ||
                    window.isKeyPress(GLFW_KEY_S) -> {
                cameraInc.z = 1f
            }

            // 左
            window.isKeyPress(GLFW_KEY_LEFT) ||
                    window.isKeyPress(GLFW_KEY_A) -> {
                cameraInc.x = -1f
            }

            // 右
            window.isKeyPress(GLFW_KEY_RIGHT) ||
                    window.isKeyPress(GLFW_KEY_D) -> {
                cameraInc.x = 1f
            }

            // 上
            window.isKeyPress(GLFW_KEY_Z) -> cameraInc.y = 1f
            window.isKeyPress(GLFW_KEY_X) -> cameraInc.y = -1f
        }
    }

    override fun update(dt: Float) {
//        degress += 1f
//        if (degress > 360f){
//            degress -= 360f
//        }
//        items[0].rotation.y = degress

       // camera.movePosition(cameraInc.mul(0.1f))

    }

    override fun render(window: Window) {
        if (window.bResize) {
            glViewport(0, 0, window.width, window.height)
            window.bResize = false
        }
        glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT)
        renderer.render(items,camera)
    }

    override fun cleanup() {
        renderer.cleanup()
//        mesh.cleanup()
    }
}

fun main() {
    val window = Window(800, 600, "hello")
    val engine = Engine(window, DrawTriangle())
    engine.start()
}